package com.zhangxiaofanfan.hotcode.third;

/**
 * <a href="https://leetcode.cn/problems/swap-nodes-in-pairs/?envType=study-plan-v2&envId=top-100-liked">两两交换链表中的节点</a>
 *
 * @author zhangxiaofanfan
 * @date 2024-05-28 23:10:55
 */
public class HotCode030 {
    public static void main(String[] args) {

    }

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode header = new ListNode();
        header.next = head;
        ListNode index1 = header;
        ListNode index2 = index1.next;
        ListNode index3 = index2.next;
        ListNode index4 = index3.next;
        while (true) {
            index1.next = index3;
            index3.next = index2;
            index2.next = index4;
            index1 = index1.next.next;
            index2 = index1.next;
            if (index2 == null) {
                break;
            }
            index3 = index2.next;
            if (index3 == null) {
                break;
            }
            index4 = index3.next;
        }
        return header.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
